package com.leesche.zcsp.node.web.intecepter.wechat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.leesche.zcsp.node.pay.wecha.service.PayService;
import com.leesche.zcsp.node.web.constants.Constants;

/**
 * 
 * @Title: verifyWechatInterceptor.java
 * @Description: TODO(验证微信内置浏览器访问)
 * @author Jerry Chen
 * @date 2016年10月21日 下午7:02:40
 * @version V1.0
 */
public class VerifyWechatInterceptor extends HandlerInterceptorAdapter {

	@Autowired
	PayService payService;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub

		if (payService.isWechatBrower(request.getHeader("User-Agent"))) {
			return true;
		}

		StringBuilder sb = new StringBuilder();
		sb.append(request.getScheme()).append("://")
				.append(request.getServerName()).append(":")
				.append(request.getServerPort())
				.append(request.getContextPath())
				.append(Constants.NOTIC_DEFAULT_ERRORVIEW_NOWECHATACCESS);

		response.sendRedirect(sb.toString());
		return false;
	}

}
